import { Get_Broker, Add_Broker, Editor_Broker, Editor_broker } from "../actionsType";
import request from "../../utils/request";
import {
    message,
} from 'antd';


const inistate = {
    Brokerlist: [],
    from: {
        name: '',
        phone: '',
        company: '',
        id: "",
    }
}


const Broker = (state = inistate, action) => {
    const { type, payload } = action;
    let { Brokerlist, from } = state;
    let newress;
    switch (type) {
        case Get_Broker: {
            Brokerlist = payload;
            return {
                ...state,
                Brokerlist
            }
        }
        case Editor_Broker:
            newress = request({
                url: "/api/editorbroker",
                method: "put",
                data: {
                    arr: { ...from },
                }
            });
            newress.then(res => {
                if (res.code === 200) {
                    message.success(res.message);
                } else {
                    message.error(res.message);
                }
            })
            return {
                ...state,
            };
        case Add_Broker: {
            newress = request({
                url: "/api/addbroker",
                method: "post",
                data: {
                    arr: { ...from },
                }
            })
            newress.then(res => {
                if (res.code === 200) {
                    message.success(res.message);
                } else {
                    message.error(res.message);
                }
            })
            return {
                ...state,
                from
            }
        }
        case Editor_broker: {
            let { type, e } = payload;
            if (type) {
                from[type] = e;
            } else {
                from = {
                    name: payload.name,
                    phone: payload.phone,
                    company: payload.company,
                    id: payload.id,
                }
            }
            return {
                ...state,
                from
            }
        }
        default:
            return { ...state };
    }
}


export default Broker